import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  UseInterceptors,
  UseGuards,
  UploadedFile,
} from '@nestjs/common';
import { MediaService } from './media.service';
import { CreateMediaDto } from './dto/create-media.dto';
import { UpdateMediaDto } from './dto/update-media.dto';
import { FileInterceptor } from '@nestjs/platform-express';
import { User } from '../user/entities/user.entity';
import { CurrentUser } from '../auth/decorators/current-user.decorator';
import { JwtStrategyGuard } from '../auth/guards/jwt.strategy.guard';
import { MediaType } from '../minio/dto/media-file-options.dto';

@UseGuards(JwtStrategyGuard)
@Controller('media')
export class MediaController {
  constructor(private readonly mediaService: MediaService) {
    console.log(mediaService);
  }

  @Post('upload')
  @UseInterceptors(FileInterceptor('file'))
  create(
    @CurrentUser() user: User,
    @UploadedFile() file: Express.Multer.File,
    @Body() createMediaDto: CreateMediaDto,
  ) {
    // console.log(user);
    // console.log(body);
    // console.log(file);
    // console.log(this.minioSerivce);
    return this.mediaService.create({
      executorId: `${user.id}`,
      file: file.buffer,
      name: createMediaDto.name || file.originalname,
      type: createMediaDto.type || MediaType.IMAGE,
    });

    // const media = {
    //   executorId: user.id,
    //   name:
    // };
    // return this.mediaService.create(file);
  }

  @Get()
  findAll() {
    return this.mediaService.findAll();
  }

  @Get(':id')
  findOne(@Param('id') id: string) {
    return this.mediaService.findOne(+id);
  }

  @Patch(':id')
  update(@Param('id') id: string, @Body() updateMediaDto: UpdateMediaDto) {
    return this.mediaService.update(+id, updateMediaDto);
  }

  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.mediaService.remove(+id);
  }
}
